package com.tianqi.familymap.http

import android.content.Context
import android.util.Log
import com.google.gson.GsonBuilder
import com.tianqi.familymap.entry.MyApp
import com.tianqi.familymap.utils.ContantUtils
import com.tianqi.familymap.utils.SingletonHolder
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitHelper(private var context: Context) {
    private val TAG = "RetrofitHelper"

    companion object : SingletonHolder<RetrofitHelper, Context>(::RetrofitHelper)

    init {
        initFamily()
    }

    private var retrofit: Retrofit? = null

    private fun initFamily(){
        Log.i(TAG, ContantUtils.BASE_URL);
        retrofit = Retrofit.Builder()
            .baseUrl(ContantUtils.BASE_URL)
            .addConverterFactory(
                GsonConverterFactory.create(
                    GsonBuilder().create()
                )
            )
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .client(MyApp.okHtpClient)
            .build();
    }

    fun getServer(): RetrofitService? {
        return retrofit!!.create(RetrofitService::class.java)
    }
}